package com.ajie.config.aop;

import com.ajie.entity.Course;
import com.ajie.entity.LookLikeFavourite;
import com.ajie.entity.Moment;
import com.ajie.entity.New;
import com.ajie.service.CourseService;
import com.ajie.service.MomentService;
import com.ajie.service.NewService;
import com.ajie.utils.DateUtils;
import com.ajie.utils.RedisUtil;
import com.ajie.utils.SecurityUtil;
import com.ajie.vo.LookLikeFavouriteVo;
import com.ajie.vo.MomentVo;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

//指定为切面类

//将该类放入Spring容器中
@Component
@Aspect
@Slf4j
public class MyAspect {
    
    
    @Autowired
    private RedisUtil redisUtil;
    
    
    //定义一个名为"myPointCut()"的切面，位置就在queryById()这个方法中
    @Pointcut("execution(public * com.ajie.controller.LookLikeFavouriteController.addLookLikeFavouriteNum(..))")
    public void myPointCut(){
        log.info("切面类1");
    }
    
    //在这个方法执行后
    @After("myPointCut()")
    public void doAfter(JoinPoint joinPoint) throws Throwable {
        Object[] objs=joinPoint.getArgs();
        LookLikeFavouriteVo obj = (LookLikeFavouriteVo)objs[0];
        String type = obj.getType();
        Long lookId = obj.getLookId();
        Long likeId = obj.getLikeId();
        Long favouriteId = obj.getFavouriteId();
        if (lookId != null){
            //浏览量
                Set<Object> set = redisUtil.sGet(type+"Id::"+lookId.toString());
                //如果非空，遍
                Boolean b = false;
                for (Object item2 : set){
                    String[] split = item2.toString().split("::");
                    String uid = split[2];
                    if (SecurityUtil.getUserId().toString().equals(uid)){
                        if ("look".equals(split[0])){
                            redisUtil.setRemove(type + "Id::"+lookId.toString(),item2.toString());
                            redisUtil.sSet(type + "Id::"+lookId.toString(), "look::userId::"+SecurityUtil.getUserId().toString()+"::createTime::"+ DateUtils.getDateTime());
                            b = true;
                        }
                    }
                }
                if (b == false){
                    redisUtil.zIncrementScore("looks",type + "::" + lookId.toString(),1);
                    redisUtil.sSet(type + "Id::"+lookId.toString(), "look::userId::"+SecurityUtil.getUserId().toString()+"::createTime::"+ DateUtils.getDateTime());
                }
            //点赞
        }
        else if (likeId != null){
            //如果非空，遍历
            Set<Object> set = redisUtil.sGet(type+"Id::"+likeId.toString());
            //如果非空，遍历
            Boolean b = false;
            for (Object item2 : set){
                String[] split = item2.toString().split("::");
                String uid = split[2];
                if (SecurityUtil.getUserId().toString().equals(uid)){
                    if ("like".equals(split[0])){
                        redisUtil.setRemove(type + "Id::"+likeId.toString(),item2.toString());
                        redisUtil.zIncrementScore("likes",type + "::" + likeId.toString(),-1);
                        b = true;
                    }
                }
            }
            if (b == false){
                redisUtil.zIncrementScore("likes",type + "::" + likeId.toString(),1);
                redisUtil.sSet(type + "Id::"+likeId.toString(), "like::userId::"+SecurityUtil.getUserId().toString()+"::createTime::"+ DateUtils.getDateTime());
            }
            //收藏
        }else if(favouriteId != null){
            Set<Object> set = redisUtil.sGet(type+"Id::"+favouriteId.toString());
            //如果非空，遍历
            Boolean b = false;
            for (Object item2 : set){
                String[] split = item2.toString().split("::");
                String uid = split[2];
                if (SecurityUtil.getUserId().toString().equals(uid)){
                    if ("favourite".equals(split[0])){
                        redisUtil.setRemove(type + "Id::"+favouriteId.toString(),item2.toString());
                        redisUtil.zIncrementScore("favourites",type + "::" + favouriteId.toString(),-1);
                        b = true;
                    }
                }
            }
            if (b == false){
                redisUtil.zIncrementScore("favourites",type + "::" + favouriteId.toString(),1);
                redisUtil.sSet(type + "Id::"+favouriteId.toString(), "favourite::userId::"+SecurityUtil.getUserId().toString()+"::createTime::"+ DateUtils.getDateTime());
            }
        }
       
    }
}
